<?php
class GoodsAttrModel extends Model{
	public $table = 'goods_attr';
	//插入商品普通属性值
	public function addGoodsAttr($gid){
		$attr = $_POST['attr'];//获得所有商品属性
		$cid = $_POST['cid'];
		foreach ($attr as $n => $value) {
			//处理checkbox radio select
			if(strstr($value,'|')){
				$arrValue = explode('|', $value);
				$avid = $arrValue[0];//得到商品值id
				$attr_value = $arrValue[1];
			}else{
				//处理 text textarea等可更改的属性
				$arrValue = explode('|', $n);
				$avid = $arrValue[1];
				$attr_value = $value;
			}
			//将 avid cid attrvalue gid插入商品属性值表
			$addData = array('attr_value'=>$attr_value,'cid'=>$cid,'gid'=>$gid,'avid'=>$avid);
			// p($addData);exit;
			$this->add($addData);
		}
		if(isset($_POST['spec'])){
			$this->addGoodsSpec($gid,$cid);
		}
		
	}
	//插入商品规格属性值
	public function addGoodsSpec($gid,$cid){
		$spec = $_POST['spec']['gg'];
		foreach ($spec as $key => $sv) {
			foreach ($sv as $n => $value) {
				if(strstr($value,'|')){
					$arrValue = explode('|', $value);
					$avid = $arrValue[0];//得到商品值id
					$attr_value = $arrValue[1];
				}else{
					//处理 text textarea等可更改的属性
					$arrValue = explode('|', $n);
					$avid = $arrValue[1];
					$attr_value = $value;
				}
				//将 avid cid attrvalue gid插入商品属性值表
				$addData = array('attr_value'=>$attr_value,'cid'=>$cid,'gid'=>$gid,'avid'=>$avid);
				$this->add($addData);
			}
		}

	}
	public function delGoodsAttr($gid){
		$this->delete('gid='.$gid);
	}
}
?>
